package com.syni.mdd.yhd.common.all.service.impl;

import com.syni.mdd.yhd.common.all.constants.Constants;
import com.syni.mdd.yhd.common.all.dao.jpa.groupChat.BmsUserChatDao;
import com.syni.mdd.yhd.common.all.entity.BmsBusiness;
import com.syni.mdd.yhd.common.all.entity.groupChat.BmsDxGroupChat;
import com.syni.mdd.yhd.common.all.entity.groupChat.BmsUserChat;
import com.syni.mdd.yhd.common.all.entity.user.BmsUser;
import com.syni.mdd.yhd.common.all.service.BmsDxGroupChatService;
import com.syni.mdd.yhd.common.all.service.groupChat.BmsUserChatService;
import com.syni.mdd.yhd.common.all.utils.SpecificationUtils;
import com.syni.mdd.yhd.common.all.utils.aurora.AuroraMessageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @className
 * @description TOO
 * @Author cfx
 * @DATE 2019/4/9 12:04
 * @VERSION 1.0
 **/
@Service
public class BmsUserChatServiceImpl extends BaseService<BmsUserChat, Integer> implements BmsUserChatService {

    @Autowired
    BmsUserChatDao bmsUserChatDao;

    @Autowired
    BmsDxGroupChatService bmsDxGroupChatService;

    @Override
    public BmsUserChatDao getBaseDao() {
        return bmsUserChatDao;
    }

    @Override
    public void deleteGroupMember(Integer businessId, Integer assistantId, Integer roleId) {
        Map<String,Object> conditionMap = new HashMap<>();
        conditionMap.put("businessId",businessId);
        conditionMap.put("userId",assistantId);
        conditionMap.put("roleId",roleId);
        List<BmsUserChat> bmsUserChats = bmsUserChatDao.findAll(new SpecificationUtils(conditionMap));
        if(bmsUserChats != null && bmsUserChats.size() > 0){
            BmsUserChat bmsUserChat = bmsUserChats.get(0);
            bmsUserChatDao.delete(bmsUserChat);
            //退出极光群
            BmsDxGroupChat bmsDxGroupChat = bmsDxGroupChatService.getBaseDao().findByIdAndIsDelete(bmsUserChat.getChatId(),Constants.COMMON_FLAG_FALSE_INT);
            if(bmsDxGroupChat != null) {
                AuroraMessageUtils.buinessUserBackGroup(bmsDxGroupChat.getGid(),bmsUserChats.get(0));
            }
        }
    }

    @Override
    public void transGroupChatManger(BmsBusiness bmsBusiness, Integer userId, Integer assistantId) {
        //插入一条新的记录
        Map<String,Object> conditionMap = new HashMap<>();
        conditionMap.put("businessId",bmsBusiness.getId());
        conditionMap.put("userId",assistantId);
        conditionMap.put("roleId", Constants.ASSISTANT_ROLE_ID);
        List<BmsUserChat> bmsUserChats = bmsUserChatDao.findAll(new SpecificationUtils(conditionMap));
        if(bmsUserChats != null && bmsUserChats.size() > 0){
            BmsUserChat bmsUserChat = bmsUserChats.get(0);
            bmsUserChat.setRoleId(Constants.BUSINESS_ROLE_ID);
            bmsUserChat.setNameInGroupChat(bmsBusiness.getVendorName());
            bmsUserChatDao.save(bmsUserChat);
            //同时将原来店员的记录删除
            /*List<BmsUserChat> bmsUserChatList = bmsUserChatDao.findByUserIdAndRoleId(assistantId,Constants.ASSISTANT_ROLE_ID);
            if(bmsUserChatList != null && bmsUserChatList.size() > 0){
                bmsUserChatDao.delete(bmsUserChatList.get(0));
            }*/
        }
    }

    @Override
    public Integer checkUserHavingGroup(Integer id, Integer roleId) {
        Integer ret = Constants.COMMON_FLAG_FALSE_INT;
        List<BmsUserChat> bmsUserChats = bmsUserChatDao.findByUserIdAndRoleId(id,roleId);
        if(bmsUserChats != null && bmsUserChats.size() > 0){
            ret = Constants.COMMON_FLAG_TRUE_INT;
        }
        return ret;
    }

    @Override
    public void reloadJGGenUser(BmsUser bmsUser, Integer roleId) {
        //直接更新到极光
        AuroraMessageUtils.updateGeneralUsers(bmsUser,roleId);
        //判断是否有群
        /*List<BmsUserChat> bmsUserChats = bmsUserChatDao.findByUserIdAndRoleId(bmsUser.getId(),roleId);
        if(bmsUserChats != null && bmsUserChats.size() > 0){
            //更新到极光
            AuroraMessageUtils.updateGeneralUsers(bmsUser,roleId);
        }*/
    }
}
